package ext.tianma.part.forms;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Locale;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import wt.fc.collections.WTArrayList;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.util.WTPropertyVetoException;
import wt.vc.wip.WorkInProgressHelper;

public class ReadExcel {
	
	private static Locale locale = null;
	private static String CLASSNAME = ReadExcel.class.getName();
	private static boolean VERBOSE = false;
	static {
		try {
			VERBOSE = (WTProperties.getLocalProperties()).getProperty("ext.tianma.excel.verbose", false);
			locale = SessionHelper.manager.getLocale();
		} catch (Throwable throwable) {
			throw new ExceptionInInitializerError(throwable);
		}
	}
	// 输出日志
	public static void outDebugInfo(String info) {
		if (VERBOSE)
			System.out.println("<<< " + CLASSNAME + "." + info);
	}
	public  static WTArrayList readSheet(String path) throws IOException, WTException, WTPropertyVetoException 
	{
		WTArrayList list = new WTArrayList();
		File file = new File(path);
		InputStream in = new FileInputStream(file);
		HSSFWorkbook wb = new HSSFWorkbook(in);
		HSSFSheet sheet = wb.getSheetAt(0);
		Hashtable ht=new Hashtable();
		String s="";
		int rowcount=sheet.getLastRowNum();
		int colcount=sheet.getRow(6).getPhysicalNumberOfCells();
		outDebugInfo("rowcount="+rowcount+"    |colcount=="+colcount);
		for(int i=5;i<rowcount+1;i++)
		{
			if(i==5)  //获取表头
			{
				
				HSSFRow row=sheet.getRow(i);
				
				for(int ii=14;ii<colcount;ii++)
				{
					HSSFCell cellj=row.getCell(ii);
					String value=getCellValue(cellj);
					outDebugInfo("表头的 value="+value);
					ht.put(s+ii,value);
				}
			}
			else
			{
				outDebugInfo("进入else  ht="+ht);
				HSSFRow row = sheet.getRow(i);
				HSSFCell cell = row.getCell(1);
				String number=getCellValue(cell);
				outDebugInfo("number="+number);
				
				HSSFCell cell1=row.getCell(9);
				String view=getCellValue(cell1);
				outDebugInfo("view="+view);
				
				WTPart part=ext.com.core.CoreUtil.getWTPartByNumberAndView(number, view);
				if(part!=null)
				{
					for(int j=14;j<colcount;j++)
					{
						
						HSSFCell cellj=row.getCell(j);
						String value=getCellValue(cellj);
						outDebugInfo("value="+value);
						String ss=s+j;
						outDebugInfo(" ss 的值：="+ss);
						
						ext.com.iba.IBAUtil.forceSetIBAValue(part,ht.get(ss).toString(),value);
					}
					
					list.add(part);
					ext.com.core.CoreUtil.checkoutObject(part);
					WTPart pa=(WTPart) WorkInProgressHelper.service.checkin(part,"批量更新");
	
					//ext.com.core.CoreUtil.checkinObject(part, "批量更新");
					outDebugInfo("-----------------------------------------end");
				}
			}
		}
		
		return list ;
		
	}
	
	
	/**
	 * 获取单元格中的信息
	 * 
	 * @param cell
	 *            单元格
	 * @return 主要参数信息
	 * @throws Exception
	 */
	public static String getCellValue(Cell cell) throws WTException {
		String value = "";
		try {
			if (null == cell) {
				return "";
			}
			switch (cell.getCellType()) {
			case XSSFCell.CELL_TYPE_NUMERIC:
				value = Double.toString(cell.getNumericCellValue());
				break;
			case XSSFCell.CELL_TYPE_STRING:
				value = cell.getStringCellValue();
				break;
			case XSSFCell.CELL_TYPE_BLANK:
				value = "";
				break;
			case XSSFCell.CELL_TYPE_BOOLEAN:
				value = Boolean.toString(cell.getBooleanCellValue());
				break;
			case XSSFCell.CELL_TYPE_FORMULA:
				try {
					value = String.valueOf(cell.getNumericCellValue());
				} catch (Exception e) {
					value = cell.getStringCellValue();
					// TODO: handle exception
				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
		}
		// System.out.println("excel 单元格的值："+value);
		if (value.indexOf(".") != -1) {
			value = value.substring(0, value.indexOf("."));
		}
		return value;
	}
}
